// 配置 store 
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import createPersistedState from "vuex-persistedstate"

// 通过 Vue.use() 安装 Vuex
Vue.use(Vuex);

const store = new Vuex.Store({
    // state存数据
    state: {
        nickname: '',
        photoImg: '',
        loginToken: '',
        editId: '',
        provincesInfo: [],
        goodsTemplateId: '',
        orderId: '',
        goodsReturnId: '',
        deviceAgentId: '',
        inboundOrderId: '',
        replenishmentOrderId: '',
        replenishmentTime: '',
        devCode: '',
        posAddr: '',
        employeeName: '',
        returnCode: '',
        priceId: '',
        backStatus: '',
        remark: ''
    },
    // vue更新store中数据的唯一方式是提交mutation
    // mutation:类似于事件,有一个事件类型和一个事件操作函数,操作函数的第一个参数是state对象
    mutations: {
        nickname: function (state, data) {
            state.nickname = data
        },
        photoImg: function (state, data) {
            state.photoImg = data
        },
        loginToken: function (state, data) {
            state.loginToken = data
        },
        editId: function (state, data) {
            state.editId = data
        },
        provincesInfo: function (state, data) {
            state.provincesInfo = data
        },
        goodsTemplateId: function (state, data) {
            state.goodsTemplateId = data
        },
        orderId: function (state, data) {
            state.orderId = data
        },
        goodsReturnId: function (state, data) {
            state.goodsReturnId = data
        },
        deviceAgentId: function (state, data) {
            state.deviceAgentId = data
        },
        inboundOrderId: function (state, data) {
            state.inboundOrderId = data
        },
        replenishmentOrderId: function (state, data) {
            state.replenishmentOrderId = data
        },
        remark: function (state, data) {
            state.remark = data
        },
        replenishmentTime: function (state, data) {
            state.replenishmentTime = data
        },
        devCode: function (state, data) {
            state.devCode = data
        },
        posAddr: function (state, data) {
            state.posAddr = data
        },
        employeeName: function (state, data) {
            state.employeeName = data
        },
        priceId: function (state, data) {
            state.priceId = data
        },
        backStatus: function (state, data) {
            state.backStatus = data
        },
    },
    // store中异步操作
    actions: {
        // context:上下文,代表当前state对象
        provinces: function (context) {
            axios.get("../../static/provinces.json")
                .then(response => {
                    context.commit('provincesInfo', response.data)
                })
                .catch(error => {
                    console.log(error);
                });
        }
    },
    plugins: [createPersistedState({
        // key: "loginToken",
        // reducer(val) {
        //     return {
        //         loginToken: val.loginToken,
        //         nickname: val.nickname,
        //         photoImg: val.photoImg,
        //     }
        // }
    })]
});
export default store;